package com.apes.scm.settle.settle.model;

import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.scm.account.tax.model.Tax;
import com.apes.scm.income.model.IncomeItem;
import com.apes.scm.masterdata.finance.model.IncomeType;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.Map;

@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = SettleRebate.CONST_TABLE_NAME)
public class SettleRebate extends BaseModel{
    public static final String CONST_TABLE_NAME = "settle_rebate";

    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "结算返利")
    @Comment("编码")
    private String id;

    @NotNull
    @ManyToOne(optional = false)
    @JoinColumn(name = "settle_id", referencedColumnName = "id")
    @Comment("结算单号")
    private Settle settle;


    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "income_item_id", nullable = false, referencedColumnName = "id")
    @Comment("收入单明细号")
    private IncomeItem incomeItem;

    @Transient
    private String incomeId;

    public String getIncomeId() {
        if(incomeItem == null) return null;
        return incomeItem.getIncome().getId();
    }


    @Transient
    private Tax tax; // 税率

    public Tax getTax() {
        if(incomeItem == null) return null;
        if (incomeItem.getIncome() == null) return null;
        return incomeItem.getTax();
    }


    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "income_type_id", referencedColumnName = "id")
    @Comment("收入类型编码")
    private IncomeType incomeType;

    @Column(name = "income_amount", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("收入单据金额")
    private double incomeAmount = 0;

    @Column(name = "pending_amount", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("可用金额")
    private double pendingAmount = 0;

    @Column(name = "actual_amount", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("扣款金额")
    private double actualAmount = 0;

    @Column(name = "amt_untaxed", nullable = false, columnDefinition = ApesConst.FIELD_16_4)
    @Comment("无税金额")
    private double amtUntaxed = 0;

    @Column(name = "amt_tax", nullable = false, columnDefinition = ApesConst.FIELD_16_4)
    @Comment("税金")
    private double amtTax = 0;

    /*
    * 税金、无税金额
    * 增加结算单返利金额,结算税金
    * */
    public void computeAmt() {
        Map result = this.getTax().computeAll(this.settle.getCompany(),this.actualAmount,1);
        amtTax = ((Double) result.get("taxAmt")).doubleValue();
        amtUntaxed = ((Double) result.get("totalExcluded")).doubleValue();
        this.settle.changeRebateAmount(actualAmount);
        this.settle.changeSettleTax(-1 * amtTax);
        this.settle.changeSettleAmount(-1 * actualAmount);
        this.settle.changeRebateTax(amtTax);
    }


}
